package cn.jdy.plugins.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.platform.entity.ConfValue;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.entity.Product;

/**
 * @author wuzhiliang
 * @title : 产品操作实现
 */
@Service
public class ProductService extends BaseDao<Product> {

	@Autowired
	private ConfService confService;

	/**
	 * 按条件查询
	 * @return
	 */
	public List<Product> query(Product product, String... date) {
		if(product == null) return super.findAll();
		if(product.getiDisplayLength() > 0){
		  return super.find(createFilter(product, date),product.getiDisplayStart(),product.getiDisplayLength());
		}
		return super.find(createFilter(product, date));
	}

	public Long count(Product product, String... date) {
		return super.count(createFilter(product, date));
	}

	/**
	 * 拼装条件
	 * @param page
	 * @return
	 */
	private DetachedCriteria createFilter(Product product, String... date) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  if(!StringUtil.isNullOrEmpty(product.getKeywords())) {
      if(!StringUtil.isNullOrEmpty(product.getKeyType())) {
        dc.add(Restrictions.like(product.getKeyType(), product.getKeywords().trim(),MatchMode.ANYWHERE));
      }else
        //TODO 添加模糊搜索
        ;
    }
	  if(date.length>0 && !StringUtil.isNullOrEmpty(date[0]) && !StringUtil.isNullOrEmpty(date[1])){
	    dc.add(Restrictions.between("createTime", date[0],date[1]));
	  }
	  if(!StringUtil.isNullOrEmpty(product.getStatus())){
	    dc.add(Restrictions.eq("status", product.getStatus()));
	  }
	  if(product.getColumnId()!=null && product.getColumnId()>0){
		  List<ConfValue> list = confService.findConfValueByPid(product.getColumnId());
		  if(list!=null && list.size()>0) {
			  int size = list.size();
			  Long[] ids = new Long[size];
			  for(int i=0; i<size; i++) {
				  ids[i] = list.get(i).getId();
			  }
			  dc.add(Restrictions.in("columnId", ids));
		  }else {
			  dc.add(Restrictions.eq("columnId", product.getColumnId()));
		  }
	  }
	  if(!StringUtil.isNullOrEmpty(product.getUserid())){
      dc.add(Restrictions.eq("userid", product.getUserid()));
    }
	  if(!StringUtil.isNullOrEmpty(product.getSortType())) {
	    dc.addOrder(Order.desc("sortType"));
	  }
	  dc.addOrder(Order.desc("createTime"));
		return dc;
	}

	/**
	 * 查询热门产品
	 * @return
	 */
	public List<Product> queryHot() {
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
    dc.addOrder(Order.desc("readTimes"));
    return super.find(dc,0,5);
		
	}
	
	/**
	 * 模糊查询产品
	 * @return
	 */
	public List<Product> queryByLike(Product product,String keywords) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  dc.add(Restrictions.eq("status", product.getStatus()));
	  dc.add(Restrictions.or(
	      Restrictions.like("title", keywords, MatchMode.ANYWHERE),
	      Restrictions.like("keyword", keywords, MatchMode.ANYWHERE),
	      Restrictions.like("summary", keywords, MatchMode.ANYWHERE),
	      Restrictions.like("context", keywords, MatchMode.ANYWHERE)
	  ));
	  dc.addOrder(Order.desc("createTime"));
	  return super.find(dc,product.getiDisplayStart(),product.getiDisplayLength());
	}

	/**
	 * 按照类型查询 产品
	 * @return
	 */
	public List<Product> findbytype(Long columnid,int firstResult, int maxResults, boolean isImg) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  if(null != columnid){
	    dc.add(Restrictions.eq("columnId", columnid));
	  }
	  if(isImg){// 是否有封面图片
	    dc.add(Restrictions.ne("imgUrl", ""));
	  }
	  dc.addOrder(Order.desc("createTime"));
	  return super.find(dc, firstResult, maxResults);
	}
	
	/**
	 * 按照大分类 查询产品
	 * @return
	 */
	public List<Product> findbyPid(Long pid,int firstResult, int maxResults, boolean isImg) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  dc.createAlias("cv", "cv");
	  if(isImg){// 是否有封面图片
	    dc.add(Restrictions.ne("imgUrl", ""));
    }
	  if(null != pid){
	    dc.add(Restrictions.eq("cv.pid", pid));
	  }
	  dc.addOrder(Order.desc("createTime"));
	  return super.find(dc,firstResult,maxResults);
	}
 
	/**
   * 删除 产品
   * @return
   */
	@Transactional
  public void del(Product product) {
    StringBuilder sql = new StringBuilder("delete ");
    sql.append(entityClass.getSimpleName());
    sql.append(" where ");
    if(!StringUtil.isNullOrEmpty(product.getUserid())){
      sql.append("userid").append(" = '").append(product.getUserid()).append("'");
      sql.append("and id").append("=").append(product.getId());
      hibernateTemplate.bulkUpdate(sql.toString(), null);
    }
    
  }

}
